package com.cadre.common.utils.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/* loaded from: MacAddressUtils.class */
public class MacAddressUtils {
    private static Boolean isLinux() {
        return Boolean.valueOf(!System.getProperty("os.name").toLowerCase().startsWith("win"));
    }

    private static String getMACAddressByLinux() throws Exception {
        Process process = Runtime.getRuntime().exec(new String[]{"ifconfig"});
        process.waitFor();
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuffer sb = new StringBuffer();
        while (true) {
            String line = br.readLine();
            if (line != null) {
                sb.append(line);
            } else {
                String result = sb.toString().split("ether")[1].trim().split("txqueuelen")[0].trim();
                br.close();
                return result;
            }
        }
    }

    private static String getMACAddressByWindows() throws Exception {
        String result = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("ipconfig /all").getInputStream(), "GBK"));
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            } else if (line.toLowerCase().indexOf("物理地址") >= 0) {
                int index = line.indexOf(":");
                if (index >= 0) {
                    result = line.substring(index + 1).trim();
                }
            }
        }
        br.close();
        return result;
    }

    public static String getMacAddress() {
        String macAddress = "";
        if (isLinux().booleanValue()) {
            try {
                macAddress = getMACAddressByLinux();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                macAddress = getMACAddressByWindows();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return macAddress;
    }

    public static void main(String[] a) throws Exception {
        if (isLinux().booleanValue()) {
            System.out.println("Linux macAddress: " + getMACAddressByLinux());
            return;
        }
        System.out.println("Windows macAddress: " + getMACAddressByWindows());
    }
}
